package com.suhui.task.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.suhui.task.common.Result;
import com.suhui.task.pojo.Task;
import com.suhui.task.service.Get_header;
import com.suhui.task.service.HttpRequest;
import com.suhui.task.service.My_util;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RequestMapping("/task")
@RestController
public class TaskController {

    private String URL = "http://172.16.249.145:5000/shproject_task";
//    private String URL = "http://192.168.136.131:3000";


    @Resource
    private HttpRequest httpRequest;

    @Resource
    private Get_header get_header;

    @Resource
    private My_util my_util;


    /**
     * 任务信息页面
     * 分页查询
     * 获取任务数据
     */
    @GetMapping("/get_task")
    public Result<?> task_data(String url,int limit,String order) {
        if ("".equals(url)){
            return Result.error("500","请求格式错误");
        }
        if (0 >= limit) {
            // 一页默认数据为 8 条
            limit = 8;
        }
        if ("".equals(order)) {
            // 默认按id 分页
            order = "id";
        }
        Map<String,Object> param = new HashMap<>();
        param.put("limit",limit);
        param.put("order",order);
        url = this.URL + url;
        // 拿到有token的请求头
        Map<String,String> header = get_header.set_header();
        String data_s = httpRequest.sendGet(url,param,header);
        // 将获取的数据 字符串 转换为 json格式的集合
        JSONArray data_array = JSON.parseArray(data_s);
        return Result.success(data_array);
    }

    /**
     * 任务信息页面
     * 普通表格查询
     * 获取总任务数量
     */
    @GetMapping("/get_task_num")
    public Result<?> task_data_total(String url) {
        if ("".equals(url)){
            return Result.error("500","请求格式错误");
        }
        url = this.URL + url;
        // 拿到有token的请求头
        Map<String,String> header = get_header.set_header();
        String data_s = httpRequest.sendGet(url,null,header);
        JSONArray data_array = JSON.parseArray(data_s);
        int total = data_array.size();
        return Result.success(total);
    }

    /**
     * 任务信息页面
     * 模糊查询
     * 按指定内容搜索数据
     */
    @GetMapping("/search")
    private Result<?> task_search(String url,String time,String group) {
        if ("".equals(url)){
            return Result.error("500","请求格式错误");
        }
        if (time==null && group==null) {
            return Result.error("500","未指定姓名或组别");
        }
        url = this.URL + url;
        Map<String,Object> param = new HashMap<>();
        if (time!=null && group==null) {
            // 时间校验
            Pattern pattern = Pattern.compile("^(\\d{4})年-(1[0-2]|0?\\d)周$");
            Matcher matcher = pattern.matcher(time);
            if (!matcher.matches()) {
                return Result.error("500","搜索时间的格式为: xxxx年-x周");
            }
            String like_time = "like.*" + time + "*";
            param.put("year_week",like_time);
        }
        if (time==null && group!=null) {
            String like_group = "like.*" + group + "*";
            param.put("group",like_group);
        }
        if (time!=null && group!=null) {
            // 时间校验
            Pattern pattern = Pattern.compile("^(\\d{4})年-(1[0-2]|0?\\d)周$");
            Matcher matcher = pattern.matcher(time);
            if (!matcher.matches()) {
                return Result.error("500","搜索时间的格式为: xxxx年-x周");
            }
            String like_group = "like.*" + group + "*";
            String like_time = "like.*" + time + "*";
            param.put("group",like_group);
            param.put("year_week",like_time);
        }
        // 拿到有token的请求头
        Map<String,String> header = get_header.set_header();
        String data_string = httpRequest.sendGet(url,param,header);
        // 将获取的数据 字符串 转换为 json格式的集合
        JSONArray data_array = JSON.parseArray(data_string);
        return Result.success(data_array);
    }

    /**
     * 任务信息页面
     * 条件查询
     * 依据id 获取具体的任务数据
     */
    @GetMapping("/task_data_byId")
    public Result<?> task_data_byId(String id,String url) {
        if (id==null || "".equals(id)) {
            return Result.error("500","无效任务的id");
        }
        id = "eq." + id;
        Map<String,Object> param = new HashMap<>();
        param.put("id",id);
        url = this.URL + url;
        // 拿到有token的请求头
        Map<String,String> header = get_header.set_header();
        //请求数据
        String data_s = httpRequest.sendGet(url,param,header);
        // 转换json对象
        JSONArray data_array = JSON.parseArray(data_s);
        Task task = new Task();
        for (Object jsonObject:data_array) {
            task = JSONObject.parseObject(jsonObject.toString(),Task.class);
        }
        return Result.success(task);
    }

    /**
     * 任务信息页面
     * 添加数据
     * 添加任务数据
     */
    @PostMapping("/add")
    public Result<?> task_add(@RequestBody Task task) {
        if (task == null) {
            return Result.error("500","无效任务数据");
        }
        // 时间校验
        Pattern pattern = Pattern.compile("^(\\d{4})年-([1-3][0-9]|0?\\d)周$");
        Matcher matcher = pattern.matcher(task.getYear_week());
        if (!matcher.matches()) {
            return Result.error("500","搜索时间的格式为: xxxx年-x周");
        }
        if (task.getGroup_detail() == null || task.getGroup_detail() == "") {
            task.setGroup_detail("暂无");
        }
        String url = this.URL + "/task";
        task.setId(my_util.getId());
        Map<String, String> header = get_header.set_header();
        String json = JSON.toJSONString(task);
        String res = httpRequest.sendPost_json_(url, json, header);
        return Result.success(res);
    }

    /**
     * 任务详情页面
     * 更新数据
     * 依据成员id更新成员数据
     */
    @PatchMapping("/update")
    public Result<?> task_update(@RequestBody Task task) {
        if (task == null) {
            return Result.error("500","无效成员数据");
        }
        // 时间校验
        Pattern pattern = Pattern.compile("^(\\d{4})年-([1-3][0-9]|0?\\d)周$");
        Matcher matcher = pattern.matcher(task.getYear_week());
        if (!matcher.matches()) {
            return Result.error("500","搜索时间的格式为: xxxx年-x周");
        }
        // url
        String url = this.URL + "/task?id=eq."+task.getId();
        // 请求token
        Map<String, String> map = get_header.set_header();
        // 数据
        HashMap<String, Object> map_data = new HashMap<>();
        if (task.getYear_week()!=null) {
            map_data.put("year_week",task.getYear_week());
        }
        if (task.getGroup()!=null) {
            map_data.put("group",task.getGroup());
        }
        if (task.getGroup_detail()!=null) {
            map_data.put("group_detail",task.getGroup_detail());
        }
        if (task.getTask_introduction()!=null) {
            map_data.put("task_introduction",task.getTask_introduction());
        }
        if (task.getTask_content()!=null) {
            map_data.put("task_content",task.getTask_content());
        }
        String data_string = httpRequest.sendPatch(url, map_data, map);
        return Result.success(data_string);
    }

    /**
     * 任务详情页面
     * 删除数据
     * 依据任务id 删除任务数据
     */
    @DeleteMapping("/delete/{id}")
    public Result<?> member_delete(@PathVariable String id) {
        if (id == null) {
            return Result.error("500","无效成员id");
        }
        // 请求token
        Map<String, String> header = get_header.set_header();
        // url： 删除指定任务数据
        String url = this.URL + "/task?id=eq."+id;
        String data_string = httpRequest.sendDelete(url, header);
        return Result.success(data_string);
    }


}
